package com.xyhuo.leetcode.y2025;

import java.util.ArrayList;
import java.util.List;

public class SummaryRanges {

    public List<String> summaryRanges(int[] nums) {
        List<String> ans = new ArrayList<>();
        if (nums.length == 0) {
            return ans;
        }
        int lastIndex = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - 1 != nums[i - 1]) {
                if (lastIndex == i - 1) {
                    ans.add(String.valueOf(nums[i - 1]));
                } else {
                    ans.add(String.format("%s->%s", nums[lastIndex], nums[i - 1]));
                }
                lastIndex = i;
            }
        }
        if (lastIndex == nums.length - 1) {
            ans.add(String.valueOf(nums[lastIndex]));
        } else {
            ans.add(String.format("%s->%s", nums[lastIndex], nums[nums.length - 1]));
        }
        return ans;
    }

    public static void main(String[] args) {
        SummaryRanges s = new SummaryRanges();
        int[] nums = {0, 2, 3, 4, 6, 8, 9};
        s.summaryRanges(nums);
    }
}
